package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.tools.corba.se.idl.constExpr.Or;
import com.zhentao.mapper.ApProductMapper;
import com.zhentao.model.common.buiness.pojo.Orders;
import com.zhentao.model.common.buiness.pojo.Product;
import com.zhentao.model.common.buiness.vo.OrdersVo;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.ApOrdersService;
import com.zhentao.mapper.ApOrdersMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author 86150
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2024-09-18 18:47:08
*/
@Service
public class ApOrdersServiceImpl extends ServiceImpl<ApOrdersMapper, Orders>
    implements ApOrdersService {
@Resource
private ApOrdersMapper apOrdersMapper;
@Resource
private ApProductMapper apProductMapper;
    @Override
    public ResponseResult orderslist(OrdersVo ordersVo) {
        List<OrdersVo> orders = apOrdersMapper.orders(ordersVo);
        return ResponseResult.okResult(orders);
    }

    @Override
    public ResponseResult ordersadd(Orders orders) {
        int num=orders.getNum();
        double price=orders.getTotalPrice();
        double total = num * price;
        orders.setTotalPrice(total);
        orders.setCreateTime(new Date());
        int insert = apOrdersMapper.insert(orders);
        if(insert==1){
            QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Orders::getId, orders.getId());
            Orders order = apOrdersMapper.selectOne(queryWrapper);
            if(order!=null){
                int productId = order.getProductId();
                int quantity = order.getNum();
                Product product = apProductMapper.selectById(productId);
                int stock = Integer.parseInt(product.getStock());
                product.setStock(String.valueOf(stock-quantity));
                apProductMapper.updateById(product);
            }
            return ResponseResult.okResult("添加成功");
        }
        return ResponseResult.errorResult(400,"添加失败");
    }

    @Override
    public ResponseResult orderststus(Orders orders) {
        if(orders.getStatus()==6){
            orders.setReceivingTime(new Date());
        }
        int i = apOrdersMapper.updateById(orders);
        if(i==1){
            return ResponseResult.okResult("修改成功");
        }
        return ResponseResult.errorResult(400,"修改失败");
    }
}




